package net.da.processor;

public abstract class AbstractProcessorMock<T extends IOResponse, E extends IORequest> implements IOProcessor<T, E> {

    protected String VALID_AUTHORISATION_CODE   = "19oG435utFzL2";
    protected String EXPIRED_AUTHORISATION_CODE = "2df23Fdd020Fd";
    protected String GUEST_AUTHORISATION_CODE =   "cFg45bd56DsG4";

    public T processRequest(E request, String authorizationCode) {
        if (VALID_AUTHORISATION_CODE.equals(authorizationCode)) {
            // Create mock authorisation session
            T response = process(request);
            response.setAuthorizationCode(authorizationCode);
            return response;
        } else if (null == authorizationCode) {
            T emptyAuthorizationCodeResponse = buildIOResponseEmptyInstance();
            emptyAuthorizationCodeResponse.setAuthorizationCode(GUEST_AUTHORISATION_CODE);
            return emptyAuthorizationCodeResponse;
        } else {
            T sessionWasExpiredResponse = buildIOResponseEmptyInstance();
            SessionExpiredError error = new SessionExpiredError();
            sessionWasExpiredResponse.addError(error);
            sessionWasExpiredResponse.setAuthorizationCode(GUEST_AUTHORISATION_CODE);
            return sessionWasExpiredResponse;
        }
    }

    protected abstract T process(E request);

    protected abstract T buildIOResponseEmptyInstance();
}
